<!--配置转换系数 机构奖惩|人员奖惩-->
<template>
  <el-dialog
    top="5vh"
    class="bonus fix-height"
    :title="title"
    :visible.sync="visible"
    width="40%"
    v-loading="true"
    :close-on-click-modal="false">
    <div class="search-form">
      <el-button type="primary" @click="async">同步依据库</el-button>
    </div>
    <el-form :model="formData" ref="formRef" v-loading="saveLoading">
        <el-table :data="formData.tableData"  class="bonus edit-table" v-loading="loading" ref="tableRef" header-align="center" @selection-change="handleSelectionChange" border :height="`calc(100vh - 320px)`">
          <el-table-column type="selection"  width="55" align="center" header-align="center">

          </el-table-column>
          <el-table-column prop="" label="序号"  width="55" align="center">
            <template slot-scope="scope">
              <span>{{scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="fl" label="分类" min-width="100" align="left" header-align="left" show-overflow-tooltip></el-table-column>

          <!--人员-->
          <el-table-column prop="fz" label="分值" min-width="100" align="center">
            <template slot-scope="scope">
              <el-form-item :prop="`tableData.${scope.$index}.fz`">
<!-- 如果需要验证加上这句话 :rules="[{validator:$validateUtil.dynamic,prop:'fz',label:'分值',row:scope.row,ruleType:'',trigger: 'change' }]"-->
                <el-input-number :controls="false" class="w100" v-model="scope.row.fz"
                                 placeholder="请输入"
                                 @change="changFz(scope.row.fz,'fz',`${scope.$index}`)"></el-input-number>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="je" label="金额(元)" min-width="100" align="center">
            <template slot-scope="scope" :prop="`tableData.${scope.$index}.je`">
              <el-input-number :controls="false" class="w100" v-model="scope.row.je" placeholder="请输入" @change="changJe(scope.row.je)"></el-input-number>
            </template>
          </el-table-column>

        </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel(false)">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="saveLoading">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getYjkList, saveYjkRatioList, asyncYjk} from '@/api/bonus/datauserrewardpunish'

export default {
  data(){
    return{
      title:'考核依据库配置转换系数',
      visible:false,
      loading:false,
      saveLoading:false,
      type:'',//人员 ry 机构 jg
      formData:{
        tableData:[]
      },
      assessTableData:[],
      chooseList:[]
    }
  },
  methods:{
    //获取考核数据
    init(type, deptId){
      this.type = type
      this.deptId = deptId ;
      this.visible=true
      this.getYjkList() ;
    },
    getYjkList(){
      this.loading = true ;
      getYjkList(this.deptId,this.type).then(res=>{
        this.formData.tableData=res.data.data
        this.loading = false ;

        this.$nextTick(() => {
          this.$refs.tableRef.doLayout() ;
        })

      }).catch(err=>{
        this.loading = false ;
      })
    },
    //配置奖金系数获取奖金项
    async(){
      this.loading = true ;
      asyncYjk(this.deptId,this.type).then(res=>{
        this.formData.tableData=res.data.data
        this.loading = false
      }).catch(err=>{
        this.loading = false ;
      })
    },
    //配置奖金系数
    handleSubmit(){
      this.saveLoading = true ;
      this.formData.tableData.map(item=>{
        item.lx = this.type
      })
      saveYjkRatioList(this.formData.tableData).then(res=>{
        if(res.data.code==0){
          this.$message.success('保存成功!')
          this.handleCancel(true)
          this.saveLoading = false ;
        }
      }).catch(err=>{
          this.saveLoading = false ;
      })
    },

    handleCancel(refresh){
      this.visible = false ;
      this.formData = {tableData: []} ;
      if (refresh){
        this.$emit('refresh')
      }
      //this.$refs.formRef.resetFields() ;
    },
    handleSelectionChange(list){
      this.chooseList = list;
    },
    changFz(val,prop,index){
      //如果需要单个单元格验证，打开注释
      // let flag = true ;
      // this.$refs.formRef.validateField(`tableData.${index}.${prop}`,valid => { //被点击的单元格的验证
      //   if (!valid){
      //     flag = true ;
      //   }else{
      //     flag = false ;
      //   }
      // });
      // if (!flag) return
      this.chooseList.forEach(ele=>{
        ele.fz = val;
      })
    },
    //批量修改值
    changJe(val){
      this.chooseList.forEach(ele=>{
        ele.je = val;
      })
    },
    //批量修改值
    changfzJg(val){
      this.chooseList.forEach(ele=>{
        ele.fzJg = val;
      })
    },
    changjeJg(val){
      this.chooseList.forEach(ele=>{
        ele.jeJg = val;
      })
    },

  }
}
</script>

<style scoped>

</style>
